package net.ys.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.ys.bean.KjResult;
import net.ys.utils.req.HttpClient;
import net.ys.utils.req.HttpResponse;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: NMY
 * @Date: 2021/6/23
 * @Time: 8:24
 */
public class Tools {

    static final int HTTP_OK = 200;

    /**
     * 钉钉通知
     */
    public static void dingTalkNotice(String content) throws Exception {
        long timestamp = System.currentTimeMillis();
        String sign = Tools.ddSign(timestamp, "SEC9823bdd4d7a287cbd8d52c9bd52767644edf9589e64c3b4b6a97ff91a4198214");
        String ddUrl = "https://oapi.dingtalk.com/robot/send?access_token=b7c6cb56c1b2096e6b6bab2b9e5d3cebd7a1ce6e6f737a50c2c9cef26b3b7508&timestamp=" + timestamp + "&sign=" + sign;
        ddNotice(ddUrl, content, "all");
    }

    public static String ddSign(long timestamp, String secret) throws Exception {
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        return URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
    }

    /**
     * 钉钉通知
     *
     * @param content
     */
    private static void ddNotice(String url, String content, String mobile) {
        JSONObject msg = new JSONObject();
        msg.put("msgtype", "text");

        JSONObject text = new JSONObject();
        text.put("content", content);

        JSONObject at = new JSONObject();
        JSONArray atMobiles = new JSONArray();
        atMobiles.add(mobile);
        at.put("atMobiles", "mobile");
        at.put("isAtAll", true);

        msg.put("text", text);
        msg.put("at", at);
        HttpResponse response = HttpClient.doPostJson(url, msg.toJSONString());
        System.out.println(response.getValue());
    }

    public static KjResult genCpResult() {
        String api = "http://apis.juhe.cn/lottery/query?key=019df520a69e64571b4f69ae56c65603&lottery_id=ssq";
        HttpResponse response = HttpClient.doGet(api);
        if (response.getCode() == HTTP_OK) {
            String value = response.getValue();
            JSONObject object = JSONObject.parseObject(value);
            int errorCode = object.getIntValue("error_code");
            if (errorCode == 0) {
                JSONObject obj = object.getJSONObject("result");
                KjResult result = new KjResult(obj.getString("lottery_date"), obj.getString("lottery_res"));
                return result;
            }
        }
        return null;
    }

    public static List<KjResult> genCpResultHis(int page) {
        String api = "http://apis.juhe.cn/lottery/history?key=11fa63aa76b55b4bb0536991680f09f9&lottery_id=ssq&page_size=50&page=" + page;
        HttpResponse response = HttpClient.doGet(api);
        if (response.getCode() == HTTP_OK) {
            String value = response.getValue();
            JSONObject object = JSONObject.parseObject(value);
            int errorCode = object.getIntValue("error_code");
            List<KjResult> results = new ArrayList<>();
            if (errorCode == 0) {
                JSONArray array = object.getJSONObject("result").getJSONArray("lotteryResList");
                if (array == null) {
                    return null;
                }
                for (int i = 0; i < array.size(); i++) {
                    JSONObject obj = array.getJSONObject(i);
                    results.add(new KjResult(obj.getString("lottery_date"), obj.getString("lottery_res")));
                }
                return results;
            }
        }
        return null;
    }
}
